package com.yanqu.road.dao.impl.activity.climbtower;

import com.alibaba.fastjson.JSON;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.climbtower.ClimbTowerTopData;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ClimbTowerTopDataDaoImpl extends TempDataObjectDao<ClimbTowerTopData> {

    public static String getClimbTowerTopDataSql = "select * from `t_c_climb_tower_top` where `activityId` in (?)";

    public Map<Integer, Map<Integer, ClimbTowerTopData>> getClimbTowerTopData(List<Integer> activityIdList) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, Map<Integer, ClimbTowerTopData>> result = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getClimbTowerTopDataSql);
            int i = 1;
            call.setString(i++, StringUtils.listToString(activityIdList, ","));
            rs = call.executeQuery();
            while (rs.next()) {
                ClimbTowerTopData data = parse(rs);
                if (!result.containsKey(data.getActivityId())) {
                    result.put(data.getActivityId(), new ConcurrentHashMap<>());
                }
                result.get(data.getActivityId()).put(data.getCellNum(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }

    public static String insertClimbTowerTopDataSql = "insert into `t_c_climb_tower_top`(activityId,cellNum,userId,serverId,pic,lv,ability,userBaseInfo) values (?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(ClimbTowerTopData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getCellNum()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getServerId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getPic()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getLv()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getAbility().toString()));
        para.put(i++, new DbParameter(Types.VARCHAR,data.getUserBaseInfo() == null ? "" : JSON.toJSONString(data.getUserBaseInfo())));
        if (getDb().executeNoneQuery(insertClimbTowerTopDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateClimbDataNoticeDataSql = "update `t_c_climb_tower_top` set userId=?,serverId=?,pic=?,lv=?,ability=?,userBaseInfo=?" +
            " where activityId=? and cellNum=?";

    @Override
    protected boolean updateData(ClimbTowerTopData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getServerId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getPic()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getLv()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getAbility().toString()));
        para.put(i++, new DbParameter(Types.VARCHAR,data.getUserBaseInfo() == null ? "" : JSON.toJSONString(data.getUserBaseInfo())));

        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getCellNum()));
        if (getDb().executeNoneQuery(updateClimbDataNoticeDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected ClimbTowerTopData parseData(ResultSet rs) throws SQLException {
        ClimbTowerTopData data = new ClimbTowerTopData();
        data.setActivityId(rs.getInt("activityId"));
        data.setCellNum(rs.getInt("cellNum"));
        data.setUserId(rs.getLong("userId"));
        data.setServerId(rs.getLong("serverId"));
        data.setPic(rs.getInt("pic"));
        data.setLv(rs.getInt("lv"));
        data.setAbility(new BigInteger(rs.getString("ability")));
        String baseInfo = rs.getString("userBaseInfo");
        if(!StringUtils.isNullOrEmpty(baseInfo)){
            data.setUserBaseInfo(JSON.parseObject(baseInfo, UserBaseInfo.class));
        }
        return data;
    }
}
